//
//  BaseRequest.m
//  Cycling
//
//  Created by cjg.mini on 2023/3/2.
//

#import "BaseRequest.h"

@implementation BaseRequest

#pragma mark -- post请求
//post带数据请求 -- 1.请求地址 2.是否缓存 3.超时loading 4.参数 5.数据 6. 结果
+ (void)basePOST:(NSString *)url cache:(BOOL)cache autoHideLoading:(BOOL)hide parms:(id)parms data:(NSData *)data result:(BaseResultBlock)resultBlock {
    
    NSString *token = [USERDEFAULTS valueForKey:@"Token"];
    NSDictionary *header = nil;
    NSString *language = [[CycLanguage sharedInstance] currentLanguage];
    if (!IsStrEmpty(token)) {
        header = @{@"token":token,@"Accept-Language":language};
    }else {
        header = @{@"Accept-Language":language};
    }
    [[BaseHttpManager defaultManager].sessionManager POST:url parameters:parms headers:header constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        //上传到服务器的数据
        if (data) {
            //上传的参数(上传图片，以文件流的格式)
            [formData appendPartWithFileData:data
                                        name:@"file"
                                    fileName:@"file.jpg"
                                    mimeType:@"image/jpg"];
        }
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self requestLog:task parms:parms responseObject:responseObject error:nil];
        resultBlock(responseObject,nil);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self requestLog:task parms:parms responseObject:nil error:nil];
        resultBlock(nil,error);
    }];
}

//post常规请求 -- 1.请求地址 2.是否缓存 3.超时loading 4.参数 5. 结果
+ (void)basePOST:(NSString *)url cache:(BOOL)cache autoHideLoading:(BOOL)hide parms:(id)parms result:(BaseResultBlock)resultBlock {
    
    [[BaseHttpManager defaultManager] configureRequestSerializer];
    NSString *token = [USERDEFAULTS valueForKey:@"Token"];
    NSDictionary *header = nil;
    NSString *language = [[CycLanguage sharedInstance] currentLanguage];
    if (!IsStrEmpty(token)) {
        header = @{@"token":token,@"Accept-Language":language};
    }else {
        header = @{@"Accept-Language":language};
    }
    NSLog(@"%@ ==****== %@ ==****== %@",url,parms,header);
    [[BaseHttpManager defaultManager].sessionManager POST:url parameters:parms headers:header progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self requestLog:task parms:parms responseObject:responseObject error:nil];
        resultBlock(responseObject , nil);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self requestLog:task parms:parms responseObject:nil error:nil];
        resultBlock(nil, error);
    }];
}

//post后台返回非json请求 -- 1.请求地址 2.是否缓存 3.超时loading 4.参数 5. 结果
+ (void)basePOST_New:(NSString *)url cache:(BOOL)cache autoHideLoading:(BOOL)hide parms:(id)parms result:(BaseResultBlock)resultBlock {
    
    NSString *token = [USERDEFAULTS valueForKey:@"Token"];
    NSDictionary *header = nil;
    NSString *language = [[CycLanguage sharedInstance] currentLanguage];
    if (!IsStrEmpty(token)) {
        header = @{@"token":token,@"Accept-Language":language};
    }else {
        header = @{@"Accept-Language":language};
    }

    [[BaseHttpManager defaultManager] configureRequestSerializerNew];
    [[BaseHttpManager defaultManager].sessionManager POST:url parameters:parms headers:header progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self requestLog:task parms:parms responseObject:responseObject error:nil];
        resultBlock(responseObject,nil);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self requestLog:task parms:parms responseObject:nil error:error];
        resultBlock(nil,error);
    }];
    
}

#pragma mark -- get请求
//get请求后台返回json类型 ---- 1.请求地址 2.是否缓存 3.超时loading 4.参数 5. 结果
+ (void)baseGET:(NSString *)url cache:(BOOL)cache autoHideLoading:(BOOL)hide parms:(id)parms request:(BaseResultBlock)resultBlock {
    
    NSString *token = [USERDEFAULTS valueForKey:@"Token"];
    NSDictionary *header = nil;
    NSString *language = [[CycLanguage sharedInstance] currentLanguage];
    if (!IsStrEmpty(token)) {
        header = @{@"token":token,@"Accept-Language":language};
    }else {
        header = @{@"Accept-Language":language};
    }

    [[BaseHttpManager defaultManager].sessionManager GET:url parameters:parms headers:header progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self requestLog:task parms:parms responseObject:responseObject error:nil];
        resultBlock(responseObject , nil);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self requestLog:task parms:parms responseObject:nil error:error];
        resultBlock(nil , error);
    }];
}

//get请求后台返回非json类型 --- 1.请求地址 2.是否缓存 3.超时loading 4.参数 5. 结果
+ (void)baseGET_New:(NSString *)url cache:(BOOL)cache autoHideLoading:(BOOL)hide parms:(id)parms request:(BaseResultBlock)resultBlock {
    [[BaseHttpManager defaultManager] configureRequestSerializerNew];
    NSString *token = [USERDEFAULTS valueForKey:@"Token"];
    NSDictionary *header = nil;
    NSString *language = [[CycLanguage sharedInstance] currentLanguage];
    if (!IsStrEmpty(token)) {
        header = @{@"token":token,@"Accept-Language":language};
    }else {
        header = @{@"Accept-Language":language};
    }

    [[BaseHttpManager defaultManager].sessionManager GET:url parameters:parms headers:header progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self requestLog:task parms:parms responseObject:nil error:error];
        resultBlock(nil , error);
    }];
    
}

#pragma mark --- 下载
+ (void)baseDownLoad:(NSString *)url cache:(BOOL)cache autoHideLoading:(BOOL)hide parms:(id)parms headers:(nullable NSDictionary<NSString *,NSString *> *)headers progess:(BaseProgressBlock)progressBlock result:(BaseResultBlock)resultBlock {
}

#pragma  mark --- 上传文件
+ (void)baseUpload:(NSString *)url parms:(id)parms imageList:(NSArray <UIImage *> *)imageList cache:(BOOL)cache autoHideLoading:(BOOL)hide progress:(BaseProgressBlock)progressBlock result:(BaseResultBlock)resultBlock {
    
    
    NSString *token = [USERDEFAULTS valueForKey:@"Token"];
    NSDictionary *header = nil;
    NSString *language = [[CycLanguage sharedInstance] currentLanguage];
    if (!IsStrEmpty(token)) {
        header = @{@"token":token,@"Accept-Language":language};
    }else {
        header = @{@"Accept-Language":language};
    }

    [[BaseHttpManager defaultManager] configureRequestSerializer];
    [[BaseHttpManager defaultManager].sessionManager POST:url parameters:parms headers:header constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        int i = 0;
        for (UIImage *image in imageList) {
            NSString *fileName = [NSString stringWithFormat:@"%d.png",i];
            NSData *imageData;
            imageData = UIImageJPEGRepresentation(image, 1.0f);
            [formData appendPartWithFileData:imageData name:@"picFile" fileName:fileName mimeType:@"image/jpg/png/jpeg"];
            i++;
        }
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        if (progressBlock) {
            progressBlock(uploadProgress);
        }
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        [self requestLog:task parms:parms responseObject:responseObject error:nil];
        resultBlock(responseObject,nil);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [self requestLog:task parms:parms responseObject:nil error:error];
        resultBlock(nil,error);
    }];
    
}
#pragma mark --- 检测网络状态
+ (void)checkoutNetWorkStatus:(void(^)(AFNetworkReachabilityStatus status))block {
    
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        block(status);
    }];
    [manager startMonitoring];
}

/// 打印数据
+ (void)requestLog:(id)task parms:(id)parms responseObject:(id)responseObject error:(NSError *)error {
    
    if (CycRequestOpenLog) {
        
        if ([task isKindOfClass:[NSURLSessionDataTask class]]) {
            DebugLog(@"请求地址：%@",((NSURLSessionDataTask*)task).currentRequest.URL.absoluteString);
        }
        else if ([task isKindOfClass:[NSURLResponse class]]) {
            DebugLog(@"请求地址：%@",((NSURLResponse*)task).URL.absoluteString);
        }
        if (parms) {
            DebugLog(@"请求参数：%@",parms);
        }
        if (responseObject) {
            DebugLog(@"服务器返回数据：%@",responseObject);
        }
        if (error) {
            DebugLog(@"Error结果：%@",error);
        }
    }
}

@end
